package com.loctek.importexport.mvp.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.loctek.importexport.R;
import com.loctek.importexport.base.BaseRecyclerAdapter;
import com.loctek.importexport.base.BaseRecyclerViewHolder;
import com.loctek.importexport.bean.getstoresiteResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EditProductAdapter extends BaseRecyclerAdapter<getstoresiteResponse.ProductInfo> {
    Map<String, getstoresiteResponse.ProductInfo> map = new HashMap<>();

    public EditProductAdapter(Context context, List<getstoresiteResponse.ProductInfo> data) {
        super(context, data);
    }

    public Map<String, getstoresiteResponse.ProductInfo> getMap() {
        return map;
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.cell_eidt_product_item;
    }

    @Override
    public void bindData(final BaseRecyclerViewHolder holder, int position, final getstoresiteResponse.ProductInfo item) {
        holder.setText(R.id.tv_productname, item.productname + "");
        holder.setText(R.id.tv_scdptypename, item.scdptypename + "");
        holder.getEditText(R.id.edit_num).setHint("0 ~ " + item.num + "");
        if (map.containsKey(item.productname)) {
            holder.getEditText(R.id.edit_num).setText(map.get(item.productname).num + "");
        } else {
            holder.getEditText(R.id.edit_num).setText(item.num + "");
        }

        ((CheckBox) holder.getView(R.id.cb_sel)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    if (!map.containsKey(item.productname)) {
                        getstoresiteResponse.ProductInfo productInfo = new getstoresiteResponse.ProductInfo();
                        productInfo.productname = item.productname;
                        productInfo.scdptypename = item.scdptypename;
                        productInfo.num = item.num;
                        productInfo.movenum = item.num;
                        map.put(item.productname, productInfo);
                    }
                    if (TextUtils.isEmpty(holder.getEditText(R.id.edit_num).getText().toString().trim())) {
                        map.get(item.productname).movenum = 0;
                    } else {
                        try {
                            map.get(item.productname).movenum = Integer.parseInt(holder.getEditText(R.id.edit_num).getText().toString().trim());
                        } catch (Exception e) {
                            map.get(item.productname).movenum = 0;
                        }
                    }
                } else {
                    if (map.containsKey(item.productname)) {
                        map.remove(map.get(item.productname));
                    }
                }
            }
        });

        holder.getEditText(R.id.edit_num).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (map.containsKey(item.productname)) {
                    if (TextUtils.isEmpty(editable.toString().trim())) {
                        map.get(item.productname).movenum = 0;
                    } else {
                        try {
                            map.get(item.productname).movenum = Integer.parseInt(editable.toString().trim());
                        } catch (Exception e) {
                            map.get(item.productname).movenum = 0;
                        }
                    }
                }
            }
        });

    }
}
